package xyz.xtt.asset.service.impl;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.api.enums.SecondEnum;
import xyz.xtt.asset.entity.TblAsset;
import xyz.xtt.asset.entity.TblAssetGroup;
import xyz.xtt.asset.mapper.TblAssetGroupMapper;
import xyz.xtt.asset.service.ITblAssetGroupService;
import xyz.xtt.asset.service.ITblAssetService;
import xyz.xtt.asset.service.IThirdService;
import xyz.xtt.asset.vo.CreateGorupResult;

/**
 * <p>
 * 赠送明细 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-22
 */
@Slf4j
@Service
public class TblAssetGroupServiceImpl extends ServiceImpl<TblAssetGroupMapper, TblAssetGroup> implements ITblAssetGroupService {
	@Autowired
	private IThirdService iThirdService;

	@Autowired
	private ITblAssetService iTblAssetService;

	@Override
	public TblAssetGroup queryAssetGroup(Long assetId) {
		LambdaQueryChainWrapper<TblAssetGroup> query = new LambdaQueryChainWrapper<>(baseMapper);
		return query.eq(TblAssetGroup::getAssetId, assetId).eq(TblAssetGroup::getState, 1).one();
	}

	@Override
	public boolean paySuccessJoinGroup(TblAsset asset, Long orderId, Long uid) {
		// 根据资产信息获取群信息
		Long rootAssetId = asset.getRootAssetId();
		if (Objects.nonNull(asset.getSecondType()) && SecondEnum.BOX_SECOND.getCode() == asset.getSecondType()) {
			TblAsset tblAsset = iTblAssetService.infoWithBizIdByCache(asset.getParentId());
			if (Objects.isNull(tblAsset)) {
				log.error("get BOX_SECOND error: assetId:{}, assetParentId:{}", asset.getId(), asset.getParentId());
				return false;
			}
			rootAssetId = tblAsset.getRootAssetId();
		}
		TblAssetGroup assetGroup = queryAssetGroup(rootAssetId);
		// 如果没有群信息，则创建群
		if (Objects.isNull(assetGroup)) {
			CreateGorupResult createGorup = iThirdService.createGorup(asset.getId(), orderId, Long.valueOf(asset.getAuthorId()), asset.getAssetName());
			if (createGorup.isResult()) {
				assetGroup = createGorup.getGroup();
			} else {
				log.info("补建群失败 asset:{}", JSONUtil.toJsonStr(asset));
			}
		}
		if (Objects.isNull(assetGroup)) {
			log.info("gift uid:{} asset:{}, orderId:{} group is null", uid, JSONUtil.toJsonStr(asset), orderId);
			return false;
		}
		iThirdService.joinGroup(orderId, uid, assetGroup.getId(), assetGroup.getThirdGroupId(), uid, 1);
		return false;
	}

	@Override
	public String getImGroupId(Long assetId) {
		LambdaQueryChainWrapper<TblAssetGroup> query = new LambdaQueryChainWrapper<>(baseMapper);
		query.eq(TblAssetGroup::getAssetId, assetId);
		TblAssetGroup assetGroup = baseMapper.selectOne(query.getWrapper());
		if (Objects.nonNull(assetGroup) && Objects.nonNull(assetGroup.getState()) && Objects.equals(assetGroup.getState(), 1)) {
			return assetGroup.getThirdGroupId();
		}
		return null;
	}
}
